package com.blog.admin.controller;

import com.blog.admin.service.SysUserService;
import com.blog.admin.vo.SysUserInfomationVO;
import com.blog.admin.vo.SysUserVO;
import com.blog.util.Result;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.blog.util.Result.beanToMap;


/**
 * 用户模块
 */
@RestController
@RequestMapping("sysUserController")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    /**
     * 分页获取用户信息
     *
     * @param request
     * @return
     */
    @RequestMapping("getUserList")
    public Map<String,Object> getUserList(HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        PageInfo<SysUserVO> result = sysUserService.getUserList(Integer.valueOf(request.getParameter("page")), Integer.valueOf(request.getParameter("limit")));
        data.put("count", result.getTotal());
        data.put("code", 0);
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysUserVO sysUserVO : result.getList()) {
            list.add(beanToMap(sysUserVO, "sysUserInfomationVO", "sysRoleVO"));
        }
        return Result.responeAjaxMap(true, "操作成功",  list, data);
    }

    /**
     * 修改用户信息
     */
    @RequestMapping("updateUser")
    public Map<String,Object> updateUser(SysUserVO sysUserVO, SysUserInfomationVO sysUserInfomationVO, HttpServletRequest request) {
       String Infomation_id = request.getParameter("sysUserInfomationId");
       String user_id = request.getParameter("sysUserInfomationId");
       sysUserInfomationVO.setId(Infomation_id);
       sysUserInfomationVO.setPkUser(user_id);
        boolean result = sysUserService.updateUser(sysUserVO,sysUserInfomationVO);
       if (result){
           SysUserVO nowuser =  (SysUserVO) SecurityUtils.getSubject().getPrincipal();
           SysUserVO newuser = sysUserService.getUserByUsername(nowuser.getUsername());
           request.getSession().setAttribute("adminUser",newuser);
       }
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 修改账号状态
     * @param sysUserVO
     * @return
     */
    @RequestMapping("updateUserType")
    public Map<String,Object> updateUserType(SysUserVO sysUserVO) {
        boolean result = sysUserService.updateUserType(sysUserVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 重置密码
     * @param sysUserVO
     * @return
     */
    @RequestMapping("resetPassword")
    public Map<String,Object> resetPassword(SysUserVO sysUserVO) {
        boolean result = sysUserService.resetPassword(sysUserVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 修改密码
     * @param sysUserVO
     * @return
     */
    @RequestMapping("updatePassword")
    public Map<String,Object> updatePassword(SysUserVO sysUserVO,String password) {
        boolean result = sysUserService.updatePassword(sysUserVO);
        return Result.responeAjaxMap(result,  result?"操作成功":"操作失败", "",null);
    }

    /**
     * 新增用户信息
     */
    @RequestMapping("insertUser")
    public Map<String,Object> insertUser(SysUserVO sysUserVO,SysUserInfomationVO sysUserInfomationVO) {
        boolean result = sysUserService.insertUser(sysUserVO,sysUserInfomationVO);
        return Result.responeAjaxMap(result, result?"操作成功":"操作失败", "",null);
    }


    /**
     * 删除用户
     */
    @RequestMapping("deleteUser")
    public Map<String,Object> deleteUser(String id) {
        if ("1a504bb4cd8c11e98cc800ffbb2df10b".equals(id)){
            return Result.responeAjaxMap(false, "超级管理员不允许删除", "",null);
        }
        boolean result = sysUserService.deleteUserById(id);
        return Result.responeAjaxMap(result, result?"操作成功":"操作失败", "",null);
    }

    /**
     * 修改个人信息
     */
    @RequestMapping("updateThisUser")
    public Map<String,Object> updateThisUser(SysUserVO sysUserVO, SysUserInfomationVO sysUserInfomationVO, MultipartFile file) {
        boolean result = sysUserService.updateUser(sysUserVO,sysUserInfomationVO);
        return Result.responeAjaxMap(result, "操作成功", "",null);
    }
}
